//TODO Agregar filtro de fecha para marcar una fecha de inicio y otra de finalizacion y mostrar solo turnos dentro de esa fecha
qx.Class.define("caraffa.widgets.GeneraPlanillas",
{
	extend : qx.ui.core.Widget,
	construct : function ()
	{
		this.base(arguments);

		this._setLayout(new qx.ui.layout.Basic());
		
		this._cmbOS = new qx.ui.form.ow.ComboFilter("Obra Social:", "", true);
			this._cmbOS.getLabel().setWidth(70);
		this._cmbPlanes = new qx.ui.form.ow.ComboFilter("Plan:", "", true);
			this._cmbPlanes.getLabel().setWidth(50);
		this._cmbClinica = new qx.ui.form.ow.ComboBox("Clinica:", [{value:"", label:"TODAS"}, {value:"1", label:"Zipoli"}, {value:"2", label:"Avellaneda"}]);
			this._cmbClinica.getLabel().setWidth(50);
		
		var layerSeleccion = new qx.ui.container.Composite(new qx.ui.layout.HBox(30));
		this._lblSeleccion = new qx.ui.basic.Label("Seleccion rapida:   ");
		this._btnTodos = new qx.ui.form.Button("Todos").set({width:85}); 
		this._btnNinguno = new qx.ui.form.Button("Ninguno").set({width:85});
		layerSeleccion.add(this._lblSeleccion);
		layerSeleccion.add(this._btnTodos);
		layerSeleccion.add(this._btnNinguno);
		
		var model = new qx.ui.table.model.Simple();
		model.setColumns(["cod_turno", "Clinica", "DNI", "Paciente", "Afiliado", "Fecha", "Cod. Prac.", "Cod. Hab.",  "Importe"]);
		model.setColumnSortable(0, false);
		
		this._grid = new qx.ui.table.Table(model);
		this._grid.getTableColumnModel().setColumnVisible(0, false);
		this._grid.getSelectionModel().setSelectionMode(qx.ui.table.selection.Model.MULTIPLE_INTERVAL_SELECTION_TOGGLE);
		this._grid.setColumnWidth(1, 80);
		this._grid.setColumnWidth(2, 80);
		this._grid.setColumnWidth(3, 200);
		this._grid.setColumnWidth(4, 90);
		this._grid.setColumnWidth(5, 80);
		this._grid.setColumnWidth(6, 100);
		this._grid.setColumnWidth(7, 100);
		this._grid.setColumnWidth(8, 100);
		
		this._grid.setShowCellFocusIndicator(true);
		this._grid.setWidth(850);
		this._grid.setHeight(370);
		this._grid.setStatusBarVisible(false);

		this._txtNroPlanilla = new qx.ui.form.ow.TextField("Nro de Planilla:");
			this._txtNroPlanilla.getLabel().setWidth(80);
			this._txtNroPlanilla.getText().setWidth(50);
		this._btnGenerarPlanilla = new qx.ui.form.Button("Generar Planilla", "caraffa/16/visto.png");
		
		this._txtNroFactura = new qx.ui.form.ow.TextField("Nro. de Factura:");
		this._txtNroFactura.setValue("");
		
		this._cmbTipoFactura =  new qx.ui.form.ow.ComboBox("Tipo:", [{value:'A'}, {value:'B'}]);
			this._cmbTipoFactura.getLabel().setWidth(50);
			this._cmbTipoFactura.getCombo().setWidth(50);
			
		this._initForm();
		
		this._add(this._cmbOS, {left:10, top:10});
		this._add(this._cmbPlanes, {left:300, top:10});
		this._add(this._cmbClinica, {left:570, top:10});
		this._add(layerSeleccion, {left:10, top:40});
		this._add(this._grid, {left:10, top:70});
		this._add(this._txtNroFactura, {left:10, top:450});
		this._add(this._cmbTipoFactura, {left:340, top:450});
		this._add(this._txtNroPlanilla, {left:480, top:450});
		this._add(this._btnGenerarPlanilla, {left:650, top:450});
		
	},
	members : 
	{
		_initForm : function ()
		{
			var rpc = new qx.io.remote.Rpc();
			rpc.setTimeout(10000);
			rpc.setUrl("services/");
			rpc.setServiceName("GeneraPlanillas");
			try
			{
				var result = rpc.callSync("getObrasSociales");
				for(var r in result)
				{
					this._cmbOS.addValue(result[r]);
				}
			}
			catch (ex)
			{
				alert(ex);
			}
			this._cmbOS.addListener("changeValue", this._getPlanes, this);
			this._cmbPlanes.addListener("changeValue", this._getDataGrid, this);
			this._cmbClinica.addListener("changeValue", this._getDataGrid, this);
			this._btnTodos.addListener("execute", this._selTodos, this);
			this._btnNinguno.addListener("execute", this._selNinguno, this);
			this._btnGenerarPlanilla.addListener("execute", this._generarPlanilla, this);
		},
		_getPlanes : function ()
		{
			this._cmbPlanes.getCombo().getChildControl("textfield").setValue("");
			var rpc = new qx.io.remote.Rpc().set({timeout:10000, url:"services/", serviceName:"GeneraPlanillas"});
			try {
				if (this._cmbOS.getValue() == "0")
				{
					this._cmbPlanes.setNewValues([{value:"0", label:'(Sin Plan)'}]);
				}
				else
				{
					var params = new Object();
					params.cod_obra_social = this._cmbOS.getValue();
					var result = rpc.callSync("getPlanes", params);
					this._cmbPlanes.setNewValues(result, true);
				}
			}
			catch (ex) {
				alert(ex);
			}
		},
		_selTodos : function ()
		{
			this._grid.getSelectionModel().setSelectionInterval(0, (this._grid.getTableModel().getRowCount()-1));
		},
		_selNinguno : function ()
		{
			this._grid.resetSelection();
			this._grid.resetCellFocus();
		},
		_generarPlanilla : function ()
		{
			var selection = [];
			this._grid.getSelectionModel().iterateSelection(function(index)
			{
				if (index !== "")
				{
					selection.push(this._grid.getTableModel().getValue(0, index));
				}
			}, this);
			if (selection.length > 0)
			{
//				if (this._txtNroFactura.getValue() != "")
//				{
					var params = new Object();
					params.registros = selection;
					params.cod_obra_social = this._cmbOS.getValue();
					params.cod_plan = this._cmbPlanes.getValue();
					params.nro_factura = this._txtNroFactura.getValue();
					params.tipo_factura = this._cmbTipoFactura.getValue();
					params.nro_planilla = this._txtNroPlanilla.getValue();
					var rpc = new qx.io.remote.Rpc();
					rpc.setTimeout(10000);
					rpc.setUrl("services/");
					rpc.setServiceName("GeneraPlanillas");
					try
					{
						var result = rpc.callSync("generaPlanilla", params);
						this._selNinguno();
						this._getDataGrid();
	
						var params = new Object();
						params.cod_planilla_cobro = result;
						params.cod_obra_social = this._cmbOS.getValue();
						var rpc1 = new qx.io.remote.Rpc();
						rpc1.setTimeout(10000);
						rpc1.setUrl("services/");
						rpc1.setServiceName("listaPlanillasOS");
						try
						{
							var result = rpc1.callSync("printPlanilla", params);
							window.open(result);
						}
						catch (ex)
						{
							alert(ex);
						}
	//					alert("Abrir pagina con el PDF de la planilla nro: " + result + " de la tabla: pla" + this._cmbOS.getValue().substr(2,this._cmbOS.getValue().length) + "\n y los registros de la tabla os" + this._cmbOS.getValue().substr(2,this._cmbOS.getValue().length) + " con el cod_planilla_cobro: " + result); 
						this._cmbOS.setValue("");
						this._txtNroFactura.setValue("");
					}
					catch (ex)
					{
						alert(ex);
					}
//				}
//				else
//				{
//					this._txtNroFactura.focus();
//					alert("Debe indicar el Nro de Factura que adjuntara a la planilla.");
//				}
			}
			else
			{
				alert("Debe seleccionar algun registro para poder generar la planilla");
			}
		},
		//TODO osocial ver que hace
		_getDataGrid : function ()
		{ 
			//ADD osocial || _getDataGrid
			if (this._cmbPlanes.getValue())
			{
				var rpc = new qx.io.remote.Rpc();
				rpc.setTimeout(10000);
				rpc.setUrl("services/");
				rpc.setServiceName("GeneraPlanillas");
				try
				{
					var params = new Object();
						params.cod_obra_social = this._cmbOS.getValue();
						params.cod_plan = this._cmbPlanes.getValue();
						params.cod_clinica = this._cmbClinica.getValue();
					var result = rpc.callSync("getPacientesAtendidos", params);
					this._grid.getTableModel().setData(result);
				}
				catch (ex)
				{
					alert(ex);
				}
			} else {
				this._cmbPlanes.setEnabled(true);
				this._grid.getTableModel().setData("");
			}
			this._selNinguno();
		}
	}
});
